#!/usr/bin/env python3
"""
TabBar 图标生成脚本
使用 PIL/Pillow 库创建简单的 PNG 图标
"""

try:
    from PIL import Image, ImageDraw
    import os
except ImportError:
    print("❌ 未安装 Pillow 库")
    print("请运行: pip install Pillow")
    print("\n或者使用浏览器打开 generate-icons.html 来生成图标")
    exit(1)

# 配置
SIZE = 81
NORMAL_COLOR = (136, 136, 136)  # #888888
ACTIVE_COLOR = (124, 77, 255)   # #7C4DFF
OUTPUT_DIR = "static/tabbar"

# 确保输出目录存在
os.makedirs(OUTPUT_DIR, exist_ok=True)

def create_star_icon(color):
    """创建星星图标"""
    img = Image.new('RGBA', (SIZE, SIZE), (255, 255, 255, 0))
    draw = ImageDraw.Draw(img)
    
    center = SIZE / 2
    outer_radius = SIZE * 0.35
    inner_radius = SIZE * 0.15
    points = []
    
    import math
    for i in range(10):
        radius = outer_radius if i % 2 == 0 else inner_radius
        angle = (i * math.pi / 5) - (math.pi / 2)
        x = center + radius * math.cos(angle)
        y = center + radius * math.sin(angle)
        points.append((x, y))
    
    draw.polygon(points, fill=color)
    return img

def create_heart_icon(color):
    """创建爱心图标"""
    img = Image.new('RGBA', (SIZE, SIZE), (255, 255, 255, 0))
    draw = ImageDraw.Draw(img)
    
    center = SIZE / 2
    radius = SIZE * 0.2
    
    # 左半圆
    draw.ellipse([center - radius * 1.5, center - radius * 0.8, 
                  center - radius * 0.5, center + radius * 0.2], fill=color)
    # 右半圆
    draw.ellipse([center + radius * 0.5, center - radius * 0.8, 
                  center + radius * 1.5, center + radius * 0.2], fill=color)
    # 三角形
    draw.polygon([
        (center - radius * 1.5, center),
        (center + radius * 1.5, center),
        (center, center + radius * 1.8)
    ], fill=color)
    
    return img

def create_clock_icon(color):
    """创建时钟图标"""
    img = Image.new('RGBA', (SIZE, SIZE), (255, 255, 255, 0))
    draw = ImageDraw.Draw(img)
    
    center = SIZE / 2
    radius = SIZE * 0.35
    
    # 圆圈
    draw.ellipse([center - radius, center - radius, 
                  center + radius, center + radius], 
                 outline=color, width=3)
    
    # 时针
    draw.line([center, center, center, center - radius * 0.6], 
              fill=color, width=3)
    # 分针
    draw.line([center, center, center + radius * 0.5, center + radius * 0.3], 
              fill=color, width=3)
    
    return img

def create_user_icon(color):
    """创建用户图标"""
    img = Image.new('RGBA', (SIZE, SIZE), (255, 255, 255, 0))
    draw = ImageDraw.Draw(img)
    
    center = SIZE / 2
    head_radius = SIZE * 0.15
    
    # 头部
    draw.ellipse([center - head_radius, center - SIZE * 0.25 - head_radius,
                  center + head_radius, center - SIZE * 0.25 + head_radius],
                 fill=color)
    
    # 身体（半圆弧）
    body_radius = SIZE * 0.25
    draw.arc([center - body_radius, center,
              center + body_radius, center + body_radius * 2],
             180, 0, fill=color, width=3)
    
    return img

# 图标配置
icons = {
    'recommend': create_star_icon,
    'favorite': create_heart_icon,
    'history': create_clock_icon,
    'user': create_user_icon
}

print("🎨 开始生成 TabBar 图标...\n")

# 生成所有图标
for name, create_func in icons.items():
    # 普通状态
    img_normal = create_func(NORMAL_COLOR)
    normal_path = os.path.join(OUTPUT_DIR, f"{name}.png")
    img_normal.save(normal_path)
    print(f"✓ 已创建: {name}.png")
    
    # 激活状态
    img_active = create_func(ACTIVE_COLOR)
    active_path = os.path.join(OUTPUT_DIR, f"{name}-active.png")
    img_active.save(active_path)
    print(f"✓ 已创建: {name}-active.png")

print("\n✅ 所有图标创建完成！")
print(f"图标保存在: {OUTPUT_DIR}")
print("\n下一步：重新编译项目以查看效果")
